package com.example.sports_meet;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import java.util.Objects;

public class LoginActivity extends AppCompatActivity {
    public static final int REQUEST_CODE_REGISTER = 1;
    private static final String TAG = "tag";
    private Button btnLogin;
    private EditText etAccount, etPassword;
    private CheckBox cbRemember;
    private String userName = "a";
    private String pass = "123";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        getSupportActionBar().setTitle("");
        Objects.requireNonNull(getSupportActionBar()).hide();
        //用户名删除按钮功能
        etAccount = findViewById(R.id.et_account);
        ImageButton deleteButton = findViewById(R.id.delete_top);

        deleteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                etAccount.setText("");
            }
        });

        //密码删除按钮功能
        etPassword = findViewById(R.id.et_password);
        ImageButton deleteNewButton = findViewById(R.id.delete_bot);

        deleteNewButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                etPassword.setText("");
            }
        });


        initView();
        initData();

        btnLogin.setOnClickListener(new View.OnClickListener() {  //登录按钮响应事件
            @Override
            public void onClick(View v) {
                //判断账号密码是否符合要求
                String account = etAccount.getText().toString();
                String password = etPassword.getText().toString();
                if (TextUtils.isEmpty(userName)) {   //如果用户名为空
                    Toast.makeText(LoginActivity.this, "还没有注册账号", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.equals(account, userName)) {  //用户名正确
                    if (TextUtils.equals(password, pass)) {  //且密码正确时
                        Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                        if (cbRemember.isChecked()) {  //如果选择了记住密码
                            SharedPreferences spf = getSharedPreferences("spfRecord", MODE_PRIVATE);
                            SharedPreferences.Editor edit = spf.edit();
                            edit.putString("account", account);
                            edit.putString("password", password);
                            edit.putBoolean("isRemember", true);
                            edit.apply();
                        } else {  //如果没选择记住密码
                            SharedPreferences spf = getSharedPreferences("spfRecord", MODE_PRIVATE);
                            SharedPreferences.Editor edit = spf.edit();
                            edit.putBoolean("isRemember", false);
                            edit.apply();
                        }
                        //实现跳转到主界面
                        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                        //传递用户名
                        intent.putExtra("account", account);
                        startActivity(intent);
                        //接收
                        LoginActivity.this.finish();

                    } else {  //用户名正确但密码不正确时
                        Toast.makeText(LoginActivity.this, "密码错误", Toast.LENGTH_SHORT).show();
                    }
                } else {  //用户名不正确时
                    Toast.makeText(LoginActivity.this, "用户名错误", Toast.LENGTH_SHORT).show();
                }
            }
        });

    }

    //记住密码
    private void initData() {
        SharedPreferences spf = getSharedPreferences("spfRecord", MODE_PRIVATE);
        boolean isRemember = spf.getBoolean("isRemember", false);
        String account = spf.getString("account", "");
        String password = spf.getString("password", "");
        //更新注册的用户名密码
        userName = account;
        pass = password;

        if (isRemember) {
            etAccount.setText(account);
            etPassword.setText(password);
            cbRemember.setChecked(true);
        }


    }

    //初始化
    private void initView() {
        btnLogin = findViewById(R.id.btn_login);
        etAccount = findViewById(R.id.et_account);
        etPassword = findViewById(R.id.et_password);
        cbRemember = findViewById(R.id.cb_remember);
    }


    //注册跳转
    public void toRegister(View view) {
        Intent intent = new Intent(this, RegisterActivity.class);

        startActivityForResult(intent, REQUEST_CODE_REGISTER);

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_REGISTER && resultCode == RegisterActivity.RESULT_CODE_REGISTER && data != null) {
            Bundle extras = data.getExtras();
            //获取用户密码
            String account = extras.getString("account", "");
            String password = extras.getString("password", "");
            etAccount.setText(account);
            etPassword.setText(password);

            //更新注册的用户名和密码
            userName = account;
            pass = password;
        }
    }

//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_login);
//
//       etAccount = findViewById(R.id.et_account);
//        ImageButton deleteButton = findViewById(R.id.delete_button);
//
//        deleteButton.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                etAccount.setText("");
//            }
//        });
//    }
}

